<?php
namespace jy_newganfee;
defined('BY_JYA') or exit('error');
/*
	订单
*/
class order extends \plugin{
	/*
		自动拼团
	*/
	public function create($mid,$fu_man=0){
		if(!$mid){
			return false;
		}
		$fu_goodsid=$this->fenxiaoset['fu_goodsid'];
		$goodsid=$this->getcolumn('goods',array('id'=>$fu_goodsid,'uniacid'=>$_SESSION['uniacid']),'goodsid');
		$goods=array(
			array('goodsid'=>$goodsid,'price'=>$fu_man,'total'=>1,'total_price'=>$fu_man),
		);
		$new_order=array(
			'mid'=>$_SESSION['uid'],
			'goodsprice'=>$fu_man,
			'remark'=>trim($post['remark']),
			'goods'=>$goods,
			'price'=>$fu_man,
			'sendtype'=>3,
		);
		$new_order['plugin_name']='jy_newganfee';
		$order_id=model('jy_weishop/order')->create($new_order);
		$new_order['id']=$order_id;
		$this->model('upfee')->addorder($new_order);
		$fu_feetype=$this->fenxiaoset['fu_feetype'];
		$params=array(
			'paytype'=>'credit'.$fu_feetype,
			'transid'=>'',
			'status'=>1,
		);
		model('jy_weishop/order')->order_pay($new_order['id'],$params);
		return $order_id;
	}
	//支付
	public function order_pay($order_id,$params,$url='',$notpl=false){
		$this->get_order($order_id);
		if(!$this->order || $this->order['ispay']){
			exi('订单不存在或者已支付！','error');
		}
		$update=array(
				'status'=>$params['status']?:1,
				'paytype'=>$params['paytype'],
				'transid'=>$params['transid'],
		);
		if($params['paytype']=='hdfk'){
			$update['ispay']=0;
		}else{
			$update['ispay']=1;
			$update['paytime']=TIMESTAMP;
		}
		foreach($update as $key=>$value){
			$this->order[$key]=$value;
		}
		pdo_update('jy_weishop_order',$update,array('id'=>$order_id));
		//更新单商品支付状态
		pdo_update('jy_weishop_order_goods',array('status'=>1,'ispay'=>$update['ispay']),array('orderid'=>$order_id));
		$noticemids=array();
		foreach($this->order['goods'] as $item){
			$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid']),array('noticemid','totalcnf','noticetype'));
			if($goodsinfo['totalcnf']==1){
				//付款减库存
				if($item['optionid']){
					pdo_update('jy_weishop_goods_option',array('stock -='=>$item['total']),array('id'=>$item['optionid']));
				}else{
					pdo_update('jy_weishop_goods',array('total -='=>$item['total']),array('id'=>$item['goodsid']));
				}
			}
			pdo_update('jy_weishop_goods',array('salerealnum +='=>1,'salenum +='=>$item['total']),array('id'=>$item['goodsid']));			
		}
		//会员等级变更
		//model('jy_weishop/member')->check_uplevel($this->order['mid'],1);
	}
	public function upstarttime($mid,$pinggoods){
		$mid=$mid?:$_SESSION['uid'];
		$pindays=pdo_get('jy_newpingfee_pindays',array('mid'=>$mid,'pingid'=>$pinggoods['id'],'uniacid'=>$_SESSION['uniacid']));		
		if($pindays['starttime']){
			$pin_days=intval($pinggoods['pin_days'])?:7;
			$endtime=$pin_days*24*3600+$pindays['starttime'];
			if($endtime<TIMESTAMP){
				pdo_update('jy_newpingfee_pindays',array('starttime'=>TIMESTAMP),array('mid'=>$mid,'pingid'=>$pinggoods['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}else{
			$insert=array(
				'mid'=>$mid,
				'pingid'=>$pinggoods['id'],
				'uniacid'=>$_SESSION['uniacid'],
				'starttime'=>TIMESTAMP,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_newpingfee_pindays',$insert);	
		}
		return false;
	}
	public function get_order($order_id,$merchid=0){
		if($this->order && $this->order['id']==$order_id){
			return $this->order;
		}
		$this->order=pdo_get('jy_weishop_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		if($merchid){
			$this->order['goods']=pdo_getall('jy_weishop_order_goods',array('orderid'=>$order_id,'merchid'=>$merchid));
		}else{
			$this->order['goods']=pdo_getall('jy_weishop_order_goods',array('orderid'=>$order_id));
		}
		foreach($this->order['goods'] as &$goods){
			$goods_data=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid']),array('title','image','type','isverify','goodssn','productsn'));
			$goods=array_merge($goods,$goods_data);
		}
		$this->order['express']=pdo_get('jy_weishop_order_express',array('orderid'=>$order_id));
		return $this->order;
	}
}